package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.odin.solution.model.request.pc.collect.CollectColumnRequestBean;
import com.homedo.odin.solution.model.request.pc.collect.CollectSolutionRequestBean;
import com.homedo.odin.solution.model.request.pc.collect.UserCollectColumnRequestBean;
import com.homedo.odin.solution.model.response.pc.collect.MyColumnCollectPageResponseBean;
import com.homedo.odin.solution.model.response.pc.collect.MyColumnCollectResponseBean;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import com.homedo.odin.solution.service.SpecialColumnServiceImpl;
import com.homedo.odin.solution.service.UserColumnServiceImpl;
import com.homedo.odin.solution.service.UserSolutionServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author: zhanglei
 * @date: 2018/4/9
 * @Description:
 */
@Api(description = "收藏", tags = {"Collects"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/collects")
public class CollectsController {

    @Autowired
    private SpecialColumnServiceImpl specialColumnService;
    @Autowired
    private SolutionServiceImpl solutionService;
    @Autowired
    private UserSolutionServiceImpl userSolutionService;
    @Autowired
    private UserColumnServiceImpl userColumnService;


    @PostMapping("/columns")
    @ApiOperation(value = "PC_收藏/取消收藏 栏目", httpMethod = "POST")
    public ResultData collectSpecialColumn(@Valid @RequestBody CollectColumnRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        specialColumnService.collectSpecialColumn(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }

    @PostMapping("/solutions")
    @ApiOperation(value = "PC_收藏/取消收藏 方案", httpMethod = "POST")
    public ResultData collectSolution(@Valid @RequestBody CollectSolutionRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        userSolutionService.collectSolution(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }

    @GetMapping("/columns")
    @ApiOperation(value = "PC_获取收藏的栏目列表", notes = "个人中心我的资讯调用", httpMethod = "GET", response = MyColumnCollectResponseBean.class)
    public ResultData getColumnCollectList(@Valid @ModelAttribute UserCollectColumnRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        MyColumnCollectPageResponseBean page = userColumnService.getColumnCollectList(requestBean);
        return ResultData.newInstance(Meta.CODE_200, page);
    }
}
